﻿@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Publications
@using SelfMadeMillionaire.Web.Infrastructure
@model PublicationAndNewsListViewModel

@{
	ViewBag.Title = "News & Publications";
	Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
	<div class="col-sm-12 col-md-12 col-lg-12">

		<!--left content block-->
		<div class="left_col_block">
			<div class="content_block_title">
				<h5>@ViewBag.Title</h5>
				<div class="title_bottom_arrow">
					<img src="@Url.Content("~/Content/images/down_arrow.png")" alt="" />
				</div>
			</div>
			@using (Html.BeginForm("Index", "Publications", FormMethod.Get, new { @class = "form-inline", id = "formFilter" }))
   {
	   @Html.ValidationSummary()
       @Html.HiddenFor(m => m.SortOrder)
				<div class="table-responsive">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td width="25%">
								<table width="100%" border="0" cellspacing="0" cellpadding="0" class="newsletter_seach_form">
									<tr>
										<td width="40%">
											@Html.LabelFor(model => model.StartDate, new { @class = "custom_label" })
										</td>
										<td width="60%">
											@Html.TextBoxFor(m => m.StartDate, "{0:" + Constants.DATE_FORMAT + "}", new { @class = "form-control", @readonly="true" })</td>
									</tr>
								</table>
							</td>

							<td width="25%">
								<table width="100%" border="0" cellspacing="0" cellpadding="0" class="newsletter_seach_form">
									<tr>
										<td width="40%">
											@Html.LabelFor(model => model.EndDate, new { @class = "custom_label" })</td>
										<td width="60%">
											@Html.TextBoxFor(m => m.EndDate, "{0:" + Constants.DATE_FORMAT + "}", new { @class = "form-control", @readonly="true" })</td>
									</tr>
								</table>
							</td>

							<td width="50%">
								<table width="100%" border="0" cellspacing="0" cellpadding="0" class="newsletter_seach_form">
									<tr>
										<td width="20%">
											@Html.LabelFor(model => model.SelectedProduct, new { @class = "custom_label" })</td>
										<td width="80%" class="3">
											@Html.DropDownListFor(m => m.SelectedProduct, Model.ProductTypes, new { @class = "form-control" })
										</td>
									</tr>
								</table>
							</td>
						</tr>

						<tr>
							<td colspan="3">
								<table width="50%" border="0" cellspacing="0" cellpadding="0" class="newsletter_seach_form">
									<tr>
										<td width="20%">
											@Html.LabelFor(model => model.Title, new { @class = "custom_label" })</td>
										<td width="80%" colspan="2">
											@Html.TextBoxFor(m => m.Title, new { @class = "form-control" })</td>
									</tr>
								</table>
							</td>
						</tr>

					</table>

					<div class="newsletter_search_btn">
						<div class="form-group">
							<button id="find-publication" type="submit" class="btn btn-primary">Find Publications</button>
						</div>
					</div>
				</div>
   }

			<div class="table_container">
				<div id="publications-list">
					@Html.Partial("PublicationsList")
				</div>
			</div>

		</div>
	</div>
</div>

<script type="text/javascript">
	$(function () {

	    RemoveTime("#@Html.IdFor(m=>m.StartDate)");
	    RemoveTime("#@Html.IdFor(m=>m.EndDate)");
	    $("#@Html.IdFor(m => m.StartDate), #@Html.IdFor(m => m.EndDate)").datepicker();

		$("#@Html.IdFor(m => m.StartDate), #@Html.IdFor(m => m.EndDate)").keypress(function (e) {
			e.preventDefault();
			$(this).datepicker();
		});
	
		$("#find-publication").click(function (e) {
			e.preventDefault();
			RefreshList();
		});
	});

    function RefreshList() {
        if (new Date($("#@Html.IdFor(m => m.StartDate)").val()) > new Date($("#@Html.IdFor(m => m.EndDate)").val())) {
            $(".validation-summary-valid")
                .addClass("validation-summary-errors")
                .removeClass("validation-summary-valid")
                .find("li").css("display", "").text("Start date must be before End Date");
            return;
        }

        if ($('#formFilter').valid()) {
            $.ajax({
                url: "@Url.Action("Index")",
	                cache: false,
	                data: {
	                    startDate: $("#@Html.IdFor(m => m.StartDate)").val(),
				        endDate: $("#@Html.IdFor(m => m.EndDate)").val(),
				        selectedProduct: $("#@Html.IdFor(m => m.SelectedProduct)").val(),
				        title: $("#@Html.IdFor(m => m.Title)").val(),
				        sort: $("#@Html.IdFor(m => m.SortOrder)").val(),
				        page: '@(Model.Publications.PageNumber.ToString())'
				    },
	                success: function (result) {
	                    $("#publications-list").html(result);

	                    $(".validation-summary-errors")
								.addClass("validation-summary-valid")
								.removeClass("validation-summary-errors")
								.find("li").css("display", "none").text("");
	                }
	            });
            }
        }

</script>

